import Vue from 'vue'
import Vuex from 'vuex'
import chat from './modules/chat'
// 导入本地持久化token的方法
import * as auth from '../utils/auth'
import { login } from '@/api/user'

console.log('* as 对象==>获取模块所有对象导出:', auth)
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 存储登录后用户 token:{token :string,refresh_token:string}
    // user: {}
    user: auth.getToken() || {}

  },
  mutations: {
    setToken (state, token) {
      // 1.存到内存
      state.user = token
      // 2.存到本地(硬盘)
      auth.setToken(token)
    },
    // 删
    delToken (state) {
      state.user = {}
      auth.delToken()
    }

  },
  actions: {
    // 登录接口请求
    /**
    *  @param {*} form {mobile:手机号, code:验证码}
    */
    async getToken ({ commit }, form) {
      const { data } = await login(form)
      console.log('token:', data)
      commit('setToken', data)
    }

  },
  modules: {
    chat
  }
})
